package com.example.hzq.zqb.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.LoginActivity;
import com.example.hzq.zqb.activity.MainActivity;
import com.example.hzq.zqb.activity.WelcomeActivity;
import com.example.hzq.zqb.bean.UserBean;
import com.example.hzq.zqb.model.Model;
import com.squareup.picasso.Picasso;

import java.util.logging.Handler;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.activity.LoginActivity.u_id;
import static com.example.hzq.zqb.activity.MainActivity.loginflag;
import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/15.
 */

public class Fragment5 extends Fragment{
    private ImageView imageView;
    private TextView name,tel,money;
    private View view;
    private Context context;
    private Button logoff;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view=inflater.inflate(R.layout.fragment5, container, false);
        imageView=(ImageView)view.findViewById(R.id.f5_mypic);
        context=getActivity();
        name=(TextView)view.findViewById(R.id.f5_username);
        tel=(TextView)view.findViewById(R.id.f5_tel);
        money=(TextView)view.findViewById(R.id.f5_money);
        logoff=(Button)view.findViewById(R.id.logoff);
        Model model=new Model();
        Call<UserBean>call=model.userInfo(u_id);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    Picasso.with(context).load(imageurl+ response.body().getHead_img()).placeholder(R.drawable.ic_default_adimage).into(imageView);
                    name.setText(response.body().getName());
                    tel.setText(response.body().getPhone());
                    money.setText(response.body().getMoney());
                }
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });
        logoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginflag=false;
                u_id=0;
                Toast.makeText(context, "注销成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Model model=new Model();
        Call<UserBean>call=model.userInfo(u_id);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    Picasso.with(context).load(imageurl+ response.body().getHead_img()).placeholder(R.drawable.ic_default_adimage).into(imageView);
                    name.setText(response.body().getName());
                    tel.setText(response.body().getPhone());
                    money.setText(response.body().getMoney());
                }
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });
    }
}
